package com.ylk.utils;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.ylk.exception.CustomerException;
import com.ylk.result.ResultEnum;
import com.ylk.exception.CustomerException;
import com.ylk.result.ResultEnum;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;

@Slf4j
public class SmsUtils {

    public final static String URL = "http://106.ihuyi.com/webservice/sms.php?method=Submit";

    public final static String HYWX_ACCOUNT="C00008787";

    public final static String HYWU_PASSWORD="06adb18ba30137f7baa8bec479dd5521";

    /**
     * 发送一键登录短信
     * @param phone
     * @param code
     */
    public static void sendLoginSms(String phone, String code) {

        try {

            //1.创建请求对象
            HttpRequest request = HttpUtil.createPost(URL);

            //2.设置请求头
            request.header("ContentType", "application/x-www-form-urlencoded;charset=GBK");

            String content = new String("您的验证码是：" + code + "。请不要把验证码泄露给其他人。");

            HashMap<String, Object> paramMap = new HashMap<>();
            paramMap.put("account", HYWX_ACCOUNT);
            paramMap.put("password", HYWU_PASSWORD);
            paramMap.put("mobile", phone);
            paramMap.put("content", content);

            request.form(paramMap);

            String response = request.execute().body();

            log.info("发送短信结果:{}", response);

            if (!response.contains("<code>2</code>")) {
                throw new CustomerException(ResultEnum.SMS_SEND_ERROR);
            }

        }catch (Exception e){
            log.error("发送短信异常:{}", e.getMessage());
            throw new CustomerException(ResultEnum.SMS_SEND_ERROR);
        }
    }

}